package com.lanou.musicbox.fragment.networkradio;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.Toast;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.lanou.musicbox.R;
import com.lanou.musicbox.adapter.networkradio.NetworkRadioAdapter;
import com.lanou.musicbox.base.BaseFragment;
import com.lanou.musicbox.bean.NetworkRadio;
import com.lanou.musicbox.bean.PlayNetRadio;
import com.lanou.musicbox.custom.Events;
import com.lanou.musicbox.entity.Entity;
import com.lanou.musicbox.utils.Contant;
import com.lanou.musicbox.utils.NetWorkUtils;
import com.lanou.musicbox.utils.VolleySingleton;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * Created by 赵宝龙 on 15/10/20.
 * 电台页面
 */
public class NetworkRadioFragment extends BaseFragment implements View.OnClickListener, AdapterView.OnItemClickListener {
    private GridView radioGridView;
    private VolleySingleton volleySingleton;
    private List<NetworkRadio.ResultEntity.ListEntity> data;
    private NetworkRadioAdapter adapter;
    private String b;
    private List<PlayNetRadio.ResultEntity.SonglistEntity> songlist;
    private int key;
    private ProgressDialog dialog;
    private boolean isConnect;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.network_radio, null);
        radioGridView = (GridView) v.findViewById(R.id.radioGridView);
        radioGridView.setOnItemClickListener(this);
        volleySingleton = VolleySingleton.getVolleySingleton(getActivity());
        isConnect = NetWorkUtils.checkNet(getActivity());
        initData();// 定义一个解析数据的方法
        return v;
    }


    private void initData() {
        data = new ArrayList<>();
        String url = Contant.Url.URL_Net_Radio_link;
        StringRequest request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                try {
                    Gson gson = new Gson();
                    NetworkRadio l = gson.fromJson(response, NetworkRadio.class);
                    data = l.getResult().getList();
                    adapter = new NetworkRadioAdapter(data, getActivity());
                    radioGridView.setAdapter(adapter);
                    if (dialog.isShowing()){
                        dialog.dismiss();
                    }
                }  catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                if (isConnect){
                    Toast.makeText(getActivity(), R.string.no_network, Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(getActivity(), R.string.connect_error, Toast.LENGTH_SHORT).show();
                }
            }
        });
        volleySingleton.addQueue(request);
        dialog=new ProgressDialog(getActivity());
        dialog.setMessage("数据加载中.....");
        dialog.show();
    }

    @Override
    public void onClick(View view) {

    }


    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
        key = position;
        String channel = data.get(position).getCh_name();
        netRadioURL(channel);


    }

    private void netRadioURL(String channelname) {
    String radio_url = Contant.Url.URL_Paly_Net_Radio_link+channelname;

        StringRequest request = new StringRequest(radio_url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Log.i("lanou9696", "onResponse_response "+response);
                Gson gson = new Gson();
                PlayNetRadio playNetRadio = gson.fromJson(response, PlayNetRadio.class);
                if (playNetRadio!=null){
                    songlist = playNetRadio.getResult().getSonglist();
                    //把歌曲 id添加到集合中
                    List<Entity> songsInfos = new ArrayList<>();
                    for (int i = 0; i < songlist.size(); i++) {
                        Entity netSongs = new Entity();
                        String song_id = songlist.get(i).getSongid();
                        String title = songlist.get(i).getTitle();
                        String pic_small = songlist.get(i).getThumb();
                        String pic_premium = songlist.get(i).getThumb();
                        String author = songlist.get(i).getArtist();

                        netSongs.setSongs_id(song_id);
                        netSongs.setTitle(title);
                        netSongs.setSmall_pic(pic_small);
                        netSongs.setBig_pic(pic_premium);
                        netSongs.setArtist(author);
                        netSongs.setItem_id(key);
                        songsInfos.add(i, netSongs);
                    }
                    String whatMusic = "网络";
                    Events.MusicInfoEvent musicInfoEvent = new Events.MusicInfoEvent();
                    musicInfoEvent.setMp3Infos(songsInfos, whatMusic);
                    EventBus.getDefault().post(musicInfoEvent);

                }




            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        volleySingleton.addQueue(request);
    }


}
